package com.ygo.feihua;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import com.ygo.feihua.BmobTable.MyUser;
import com.ygo.feihua.BmobTable.Tiezi;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.support.v4.util.ArrayMap;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.view.MenuItem;
import android.view.Menu;
import com.ygo.feihua.adapter.*;
import com.ygo.feihua.util.*;
import android.support.v7.widget.*;

public class YonghuMingpian extends AppCompatActivity
{
    ImageView mp_tx;
    TextView mp_name,mp_qm;
    Gongju gj;
    private RecyclerView mp_rec;
    ShequAdapter adapter;
    List<Tiezi> data=new ArrayList<Tiezi>();
    String txuri="";
	MyUser mu;
	private DialogUtils du;
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO: Implement this method
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yonghu_mingtpian);
        gj=Gongju.getdx(this);
		du=DialogUtils.getdx(this);
        du.dialogj1(false,"","加载中");
        mp_tx=(ImageView) findViewById(R.id.mp_tx);
        mp_name=(TextView) findViewById(R.id.mp_name);
        mp_qm=(TextView) findViewById(R.id.mp_qm);
        mp_rec=(RecyclerView) findViewById(R.id.mp_rec);
		
		gj.cshToolbar((Toolbar) findViewById(R.id.toolbar),"用户名片");
        Intent intent=getIntent();
        String id=intent.getStringExtra("id");
		
		String name=intent.getStringExtra("name");
		
		mp_tx.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					if(!txuri.equals("")){
					Intent intent1 =new Intent(YonghuMingpian.this,ImageXiangxi.class);
					intent1.putExtra("uri",txuri);
					startActivity(intent1);
					}
					// TODO: Implement this method
				}
		});
		
		if(name==null){
		
        BmobQuery<MyUser> query = new BmobQuery<MyUser>();
        query.getObject(id, new QueryListener<MyUser>() {

				@Override
				public void done(final MyUser p1, BmobException p2)
				{
					if(p2==null){
					
                        BmobQuery<Tiezi> bq=new BmobQuery<Tiezi>();
                        bq.addWhereEqualTo("tiezuo",p1);
                        bq.order("-createdAt");
                        bq.include("tiezuo");// 希望在查询帖子信息的同时也把发布人的信息查询出来
                        
                        
                        bq.findObjects(new FindListener<Tiezi>(){

                                @Override
                                public void done(List<Tiezi> ti, BmobException p2)
                                {
                                    if(p2==null){
										mu=p1;
                                        for(final Tiezi tt:ti){
                                            data.add(tt);  
                                        }			
                                        mp_name.setText(p1.getUsername());
                                        mp_qm.setText(p1.getsignature());
                                    	txuri=gj.setTouxiang(mp_tx,p1);
									  	mp_rec.setLayoutManager(new LinearLayoutManager(YonghuMingpian.this));
                                        adapter = new ShequAdapter(YonghuMingpian.this, data);
                                        mp_rec.setAdapter(adapter);
                                        du.dis();
                                        
                                   }else{
                                       du.dis();
									   gj.toastBmobException(mp_rec,"加载失败",p2);
									   
                                   //    gj.show("加载失败"+p2);
									//   finish();
                                   }
                                    // TODO: Implement this method
                                }
                            });		
					}else{
						du.dis();
						gj.toastBmobException(mp_rec,"加载失败",p2);
						
						//gj.show("加载失败"+p2);
					//	finish();
					}
					// TODO: Implement this method
				}
            });
			}else{
				BmobQuery<MyUser> query = new BmobQuery<MyUser>();
				query.addWhereEqualTo("username",name);
				query.findObjects(new FindListener<MyUser>(){

						@Override
						public void done(List<MyUser> p1, BmobException p2)
						{
							if(p2==null){
								mp_name.setText(p1.get(0).getUsername());
								mp_qm.setText(p1.get(0).getsignature());
								txuri=gj.setTouxiang(mp_tx,p1.get(0));
								
								du.dis();
							}else{
								du.dis();
								gj.toastBmobException(mp_rec,"加载失败",p2);
								
							//	gj.show("加载失败"+p2);
							}
							// TODO: Implement this method
						}
					});	
			}
    }
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method
		getMenuInflater().inflate(R.menu.menu_mingpian, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId()){
			case R.id.action_wz:
				if(mu.getUserpoint()!=null&&mu.getUserpoint().getLatitude()!=0&&mu.getUserpoint().getLongitude()!=0){
				
					 Intent intent=new Intent(YonghuMingpian.this,KadianDitu.class);
					 intent.putExtra("dk",4);
					 intent.putExtra("jd",mu.getUserpoint().getLongitude());
					 intent.putExtra("wd",mu.getUserpoint().getLatitude());
					 startActivity(intent);
				}else{
					gj.show("未找到该用户位置信息");
				}
				break;
			
		}
		
		// TODO: Implement this method
		return super.onOptionsItemSelected(item);
	}

	
	
}
